<template>
  <section class="section-list">
    <section-edit :visible.sync="dialogVisible" @listPage="listPage" :pid="pid"></section-edit>
    <el-button type="primary" round @click="dialogVisible=true">新建板块</el-button>
    <el-tag type="success">{{pname}}</el-tag>
    <el-table :data="tableData" stripe height="calc(100vh - 215px)" :style="{marginTop:'10px'}">
      <el-table-column prop="Id" label="ID" width="100">
      </el-table-column>
      <el-table-column prop="Name" label="标题" width="200">
      </el-table-column>
      <el-table-column label="题目ID">
        <template slot-scope="scope">
          <span>{{scope.row.Qids}}</span>
        </template>
      </el-table-column>
      <el-table-column label="题目数量" width="80">
        <template slot-scope="scope">
          <span>{{scope.row.Qids.length}}</span>
        </template>
      </el-table-column>
      <el-table-column label="日期" width="240">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{scope.row.CreatedAt}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click.native.prevent="goQuestion(scope.row)"
                     type="text" size="small">
            进入试题<i class="el-icon-d-arrow-right"></i>
          </el-button>
          <el-button @click.native.prevent="del(scope.row)"
                     type="text" size="small">
            删除<i class="el-icon-d-arrow-right"></i>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :style="{marginTop:'10px'}"
      @size-change="sizeChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 40, 100]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      background
      @current-change="listPage"
      :total="total">
    </el-pagination>
  </section>
</template>

<script>
import {SectionService} from '@/service/api'
import EventBus from '@/service/event'
import SectionEdit from './Edit'

export default {
  components: {
    SectionEdit
  },
  data: function () {
    return {
      total: 0,
      tableData: [],
      section: {},
      pageSize: 10,
      currentPage: 1,
      dialogVisible: false
    }
  },
  computed: {
    pid: function () {
      return this.$route.query.pid
    },
    pname: function () {
      return this.$route.query.pname
    }
  },
  mounted: function () {
    this.listPage()
    EventBus.$emit('acticePath', 'section')
  },
  methods: {
    sizeChange: function (val) {
      this.pageSize = val
      this.listPage()
    },
    listPage: function (val) {
      SectionService.page(val, this.pageSize, this.pid)
        .then(res => {
          if (res) {
            this.tableData = res.sections
            this.total = res.total
          }
        })
    },
    goQuestion: function (item) {
      this.$router.push({path: '/question', query: {sid: item.Id}})
    },
    del: function (item) {
      // todo delete
    }
  }
}
</script>

<style scoped>
</style>
